﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Entities;
using DigitalAnts.App_Code.BAL.Components;
using System.Data;
using DigitalAnts.App_Code.BAL.Utils;
using VMCLibrary.RegexServiceProvider;


namespace DigitalAnts.Admin
{
    public partial class AddProducer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Login() == false)
            {
                Response.Redirect("~/Admin/EmployeeLogin.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    lbl_addupt.Text = "Add new";
                    showProducer();
                    SelectUpdate();
                }
            }
        }
        public bool Login()
        {
            clsEmployee obj = new clsEmployee();
            obj.UserName = Session["AdminUser"].ToString();
            obj.Role = Session["Role"].ToString();
            return clsEmployeeLoginBAL.queryEmployeeLogin(obj);
        }

        public void addNew()
        {
            List<bool> val = new List<bool>();
            RegexSpecificConfigManager validateUser = new RegexSpecificConfigManager();
            val.Add(validateUser.ValidateCustomUserName(txt_producer.Text.Trim()));
            if (val.IndexOf(false) >= 0)
            {
                lbl_mess.Text = "Dữ liệu lỗi";
            }
            else{
            clsProducer obj = new clsProducer();
            obj.ProducerName = txt_producer.Text.Trim();
            if (clsProducerBAL.CheckProducerExist(obj) == true)
                obj.Description = txt_description.Text.Trim();
            obj.Avatar = "~/UploadFiles/" + ful_image.FileName;
            if (clsProducerBAL.doAddNewProducer(obj) == true && clsUploadImage.uploadProductImage

(ful_image) == true)
            {
                lblname.Text = "Tên hãng xe đã tồn tại";
            }
            else
            {
                obj.Description = txt_description.Text.Trim();
                obj.Avatar = "~/UploadFiles/" + ful_image.FileName;
                if (clsProducerBAL.doAddNewProducer(obj) == true && clsUploadImage.uploadProductImage

(ful_image) == true)
                {
                    lbl_mess.Text = "Add new success";
                }
                else { lbl_mess.Text = "Add new fail"; }
            }
            }
        }

        protected void btt_add_Click(object sender, EventArgs e)
        {
            if (Request["ProducerID"] == null)
            {
                addNew();
                showProducer();
            }
            else
            {
                List<bool> val = new List<bool>();
                RegexSpecificConfigManager validateUser = new RegexSpecificConfigManager();
                val.Add(validateUser.ValidateCustomUserName(txt_producer.Text.Trim()));
                if (val.IndexOf(false) >= 0)
                {
                    lbl_mess.Text = "Dữ liệu lỗi";
                }
                else
                {
                    clsProducer obj = new clsProducer();
                    if (ful_image.FileName != null)
                    {
                        obj.Avatar = ful_image.FileName;
                        clsUploadImage.uploadProductImage(ful_image);

                    }
                obj.ProducerID = Convert.ToInt32(Request["ProducerID"].ToString());
                obj.ProducerName = txt_producer.Text.Trim();
                obj.Description = txt_description.Text.Trim();

                if (clsProducerBAL.doUpdateProducer(obj) == true )
                {
                    lbl_mess.Text = "Cập nhật thông tin thành công";
                    showProducer();
                }
                else { lbl_mess.Text = "Cập nhật thất bại"; }
                }
            }
        }
        public void showProducer()
        {
            gv_producer.DataSource = clsProducerBAL.doProducerSelectList();
            gv_producer.DataBind();
        }

        public void SelectUpdate()
        {
            if (Request["ProducerID"] != null)
            {
                lbl_addupt.Text = "Bạn đang Sửa";
                int id = Convert.ToInt32(Request["ProducerID"].ToString());
                clsProducer obj = new clsProducer();
                obj.ProducerID = id;
                DataTable dt = clsProducerBAL.doProductSelectID(obj);
                txt_producer.Text = dt.Rows[0]["colProducer"].ToString();
                txt_description.Text = dt.Rows[0]["colDescription"].ToString();
            }
        }

        protected void gv_producer_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv_producer.PageIndex = e.NewPageIndex;
            gv_producer.DataBind();
        }
    }
}